package client.output.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import client.constants.output.gui.IGuiCaptions;
import client.output.gui.controller.AChatController;

public class ChatWindow extends JFrame {

	private static final long serialVersionUID = -2837015412520472827L;

	private JTextArea taChatArea;
	private JScrollPane spChatArea;
	private JTextField tfInputField;
	private JButton btnSend;

	private AChatController controller;

	public ChatWindow() {
		super(IGuiCaptions.WINDOW_TITLE);

		setDefaultCloseOperation(EXIT_ON_CLOSE);

		initComponents();

		pack();
		centerWindow();
	}

	private void centerWindow() {
		Dimension windowDim = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameDim = getSize();

		this.setLocation((windowDim.width - frameDim.width) / 2,
				(windowDim.height - frameDim.height) / 2);
	}

	private void initComponents() {
		setResizable(false);

		setLayout(new GridBagLayout());
		GridBagConstraints constraints = new GridBagConstraints();

		JPanel innerPanel = new JPanel(new GridBagLayout());
		constraints.insets = new Insets(5, 5, 5, 5);
		add(innerPanel, constraints);
		constraints.insets = new Insets(0, 0, 0, 0);

		this.taChatArea = new JTextArea();
		this.taChatArea.setEditable(false);
		this.taChatArea.setLineWrap(true);
		this.taChatArea.setAutoscrolls(true);
		this.taChatArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		this.taChatArea.setColumns(60);
		this.taChatArea.setRows(20);
		// this.taChatArea.setPreferredSize(new Dimension(662, 322));

		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.fill = GridBagConstraints.NONE;

		this.spChatArea = new JScrollPane();
		this.spChatArea.setViewportView(taChatArea);
		innerPanel.add(this.spChatArea, constraints);

		this.tfInputField = new JTextField();

		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.fill = GridBagConstraints.NONE;
		constraints.anchor = GridBagConstraints.LAST_LINE_START;
		constraints.ipadx = 565;
		constraints.ipady = 7;
		constraints.insets = new Insets(5, 0, 0, 0);
		innerPanel.add(tfInputField, constraints);
		constraints.ipady = 0;
		constraints.insets = new Insets(0, 0, 0, 0);

		this.btnSend = new JButton(IGuiCaptions.BTN_SEND);

		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.fill = GridBagConstraints.NONE;
		constraints.anchor = GridBagConstraints.LAST_LINE_END;
		constraints.ipadx = 20;
		constraints.insets = new Insets(0, 0, 1, 0);
		innerPanel.add(btnSend, constraints);

		getRootPane().setDefaultButton(btnSend);
	}

	private void initListener() {
		btnSend.addActionListener(controller);
		taChatArea.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent e) {
				//
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				taChatArea.setCaretPosition(taChatArea.getText().length());
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				//
			}
		});
	}

	public String getInput() {
		String toReturn = null;

		if (this.tfInputField != null) {
			toReturn = !("".equals(this.tfInputField.getText())) ? this.tfInputField
					.getText()
					: null;
			this.tfInputField.setText("");
		}

		return toReturn;
	}

	public void writeMessage(String message) {
		this.taChatArea.append(message + "\n");
	}

	public void addController(AChatController controller) {
		this.controller = controller;

		initListener();
	}
}
